#include "e.h"
#include "e_mod_main.h"
#include "config.h"

typedef struct _cfdata CFData;
typedef struct _Cfg_File_Data Cfg_File_Data;

struct _cfdata 
{
   int allow_overlap;
   char* theme; 
   char* themelist[20];
   int themenumber;
   char* charset;
   char* charsetlist[4];
   int charsetnumber;
   int charsetselected;
   int player;   
   double poll_time;
};

struct _Cfg_File_Data 
{
   E_Config_Dialog *cfd;
   char *file;
};

/* Protos */
static Evas_Object   *_create_widgets(E_Config_Dialog *cfd, Evas *evas, Config *cfdata);
static void          *_create_data(E_Config_Dialog *cfd);
static void          _free_data(E_Config_Dialog *cfd, CFData *cfdata);
static Evas_Object   *_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, CFData *cfdata);
static int           _basic_apply_data(E_Config_Dialog *cfd, CFData *cfdata);
static Evas_Object   *_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, CFData *cfdata);
static int           _advanced_apply_data(E_Config_Dialog *cfd, CFData *cfdata);

void
_config_eloquence_module(E_Container *con, Eloquence *eloquence) 
{
   E_Config_Dialog *cfd;
   E_Config_Dialog_View *v;
	 char buf[4096];

   v = E_NEW(E_Config_Dialog_View, 1);
   
   v->create_cfdata = _create_data;
   v->free_cfdata = _free_data;
   v->basic.apply_cfdata = _basic_apply_data;
   v->basic.create_widgets = _basic_create_widgets;
   v->advanced.apply_cfdata = _advanced_apply_data;
   v->advanced.create_widgets = _advanced_create_widgets;
   
	 snprintf(buf, sizeof(buf), "%s/module.edj", 
					 e_module_dir_get(eloquence->module));
   cfd = e_config_dialog_new(e_container_current_get(e_manager_current_get()), 
					 _("Eloquence Configuration"), 
					 "E", "_e_mod_eloquence_dialog", 
					 buf, 0, v, eloquence);
   eloquence->config_dialog = cfd;
}

static void
_fill_data(Eloquence *eloquence, CFData *cfdata) 
{
   Eloquence_Face *face = eloquence->faces->data;

   cfdata->allow_overlap = face->conf->allow_overlap;
   cfdata->poll_time = face->conf->poll_time;
   cfdata->player = face->conf->player;
   cfdata->theme = face->conf->theme;
   cfdata->themenumber = 19;
   cfdata->charset = face->conf->input_charset;
   
/* to add a new locale here create a new entry, following the form below *
 * and increase the numbers of `charsetnumber` and `charsetselected`.    *
 * Don't forget that you also need to increase the charsetlist-array.    *
 * Have in mind that in C arrays are starting to at 0, and the fourth    *
 * position has number three and so on.                                  */ 
   cfdata->charsetlist[4];
   cfdata->charsetnumber = 3;
   cfdata->charsetselected = 3;
   cfdata->charsetlist[0] = "ISO-8859-1";
   cfdata->charsetlist[1] = "ISO-8859-15";
   cfdata->charsetlist[2] = "UTF-8";
   cfdata->charsetlist[3] = "GBK";
}

static void
*_create_data(E_Config_Dialog *cfd) 
{
   CFData *cfdata;
   Eloquence *e;
   
   e = cfd->data;
   cfdata = E_NEW(CFData, 1);
   _fill_data(e, cfdata);
   return cfdata;
}

static void
_free_data(E_Config_Dialog *cfd, CFData *cfdata) 
{
   Eloquence *eloquence;

   eloquence = cfd->data;
   eloquence->config_dialog = NULL;
   free(cfdata);
}

static Evas_Object
*_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, CFData *cfdata) 
{
   Eloquence *e;
   Evas_Object *o, *of, *ob;
   
   e = cfd->data;
   _fill_data(e, cfdata);

   o = e_widget_list_add(evas, 0, 0);
   ob = e_widget_label_add(evas, _("Check for song every:"));
   e_widget_list_object_append(o, ob, 1, 1, 0.5);
   ob = e_widget_slider_add(evas, 1, 0, _("%1.1f seconds"),
		   0.5, 10.0, 0.1, 0, &(cfdata->poll_time), NULL, 200);
   e_widget_list_object_append(o, ob, 1, 1, 0.5);
   return o;
}

static int 
_basic_apply_data(E_Config_Dialog *cfd, CFData *cfdata) 
{
   Eloquence *eloquence = cfd->data;;
   Eloquence_Face *face = eloquence->faces->data;

   face->old_player = face->conf->player;
   face->conf->poll_time = cfdata->poll_time;
   
   e_config_save_queue();
   
   _eloquence_face_cb_config_updated(face);
   return 1;
}

static Evas_Object
*_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, CFData *cfdata) 
{
   Evas_Object *o, *of, *ob, *ot;
   E_Radio_Group *rg, *rg2, *rg3;
   Eloquence *e = cfd->data;;
   int theme_found = 0;
   char path[PATH_MAX] = PACKAGE_DATA_DIR"/themes/";

   _fill_data(e, cfdata);

   o = e_widget_list_add(evas, 0, 0);
   
   of = e_widget_framelist_add(evas, _("General Settings"), 0);
   ob = e_widget_label_add(evas, _("Check for song every:"));
   e_widget_framelist_object_append(of, ob);
   ob = e_widget_slider_add(evas, 1, 0, _("%1.1f seconds"),
		   0.5, 10.0, 0.1, 0, &(cfdata->poll_time), NULL, 200);
   e_widget_framelist_object_append(of, ob);
   e_widget_list_object_append(o, of, 1, 1, 0.5);
  
   of = e_widget_framelist_add(evas, _("Player Selection"), 0);
   rg = e_widget_radio_group_new(&(cfdata->player));
#ifdef HAVE_XMMS_XMMSCTRL_H
   ob = e_widget_radio_add(evas, _("XMMS"), PLAYER_XMMS, rg);
   e_widget_framelist_object_append(of, ob);
#endif   
#ifdef HAVE_XMMSCLIENT_XMMSCLIENT_H
   ob = e_widget_radio_add(evas, _("XMMS2"), PLAYER_XMMS2, rg);
   e_widget_framelist_object_append(of, ob);
#endif
#ifdef HAVE_BMP_BEEPCTRL_H
   ob = e_widget_radio_add(evas, _("Beep Media Player"), PLAYER_BMP, rg);
   e_widget_framelist_object_append(of, ob);
#endif
#ifdef HAVE_BMPX_METADATA_H
   ob = e_widget_radio_add(evas, _("BMPx"), PLAYER_BMPX, rg);
   e_widget_framelist_object_append(of, ob);
#endif
#ifdef HAVE_AUDACIOUS_BEEPCTRL_H
   ob = e_widget_radio_add(evas, _("Audacious"), PLAYER_AUDACIOUS, rg);
   e_widget_framelist_object_append(of, ob);
#endif
#ifdef HAVE_LIBMPD_LIBMPDCLIENT_H
   ob = e_widget_radio_add(evas, _("MPD"), PLAYER_MPD, rg);
   e_widget_framelist_object_append(of, ob);
#endif
#ifdef HAVE_AMAROK
   ob = e_widget_radio_add(evas, _("amaroK"), PLAYER_AMAROK, rg);
   e_widget_framelist_object_append(of, ob);
#endif   
   e_widget_list_object_append(o, of, 1, 1, 0.5);
   
   of = e_widget_framelist_add(evas, _("Theme Selection"), 0);
   rg2 = e_widget_radio_group_new(&(cfdata->themenumber));
   Ecore_List *files = ecore_file_ls(path);
   if (files){
	   // pointer to the file part
	   char *file_in_path = path + strlen(path);
	   char *file;
	   while ((file = ecore_list_next(files))){
		   if (file[0] == '.')
			   continue; // skip (hide) dot-files
		   strcpy(file_in_path, file);
		   if (ecore_file_is_dir(path))
			   continue; // no sub-directories
		   char *extension = strrchr(file, '.');
		   if (extension && strcasecmp(extension, ".edj") == 0){
			   *extension = '\0'; // strip extension
			   cfdata->themelist[theme_found] = evas_stringshare_add(file);
			   if (strcmp(file, cfdata->theme) == 0)
				   cfdata->themenumber = theme_found;
			   ob = e_widget_radio_add(evas, _(file), theme_found, rg2);
			   e_widget_framelist_object_append(of, ob);
			   theme_found++;
		   }
	   }
	   ecore_list_destroy(files);
   }
   if (!theme_found)
	   printf("eloquence: no themes in %s!\n", PACKAGE_DATA_DIR"/themes");
   e_widget_list_object_append(o, of, 1, 1, 0.5);
  
   of = e_widget_framelist_add(evas, _("Input Charset Selection"), 0);
   rg3 = e_widget_radio_group_new(&(cfdata->charsetselected));
   int i;
   for (i = 0; i <= cfdata->charsetnumber; i++){
	   if (strcmp(cfdata->charsetlist[i], cfdata->charset) == 0)
		   cfdata->charsetselected = i;
	   ob = e_widget_radio_add(evas, _(cfdata->charsetlist[i]), i, rg3);
	   e_widget_framelist_object_append(of, ob);
   }
   e_widget_list_object_append(o, of, 1, 1, 0.5);

/*
   of = e_widget_framelist_add(evas, _("Extras"), 0);
   ob = e_widget_check_add(evas, _("Allow Overlap"), &(cfdata->allow_overlap));
   e_widget_framelist_object_append(of, ob);
   e_widget_list_object_append(o, of, 1, 1, 0.5);
*/
   
   return o;
}

static int 
_advanced_apply_data(E_Config_Dialog *cfd, CFData *cfdata) 
{
   Eloquence *eloquence = cfd->data;
   Eloquence_Face *face = eloquence->faces->data;
   
   face->old_player = face->conf->player;
   face->conf->allow_overlap = cfdata->allow_overlap;
   face->conf->poll_time = cfdata->poll_time;
   face->conf->player = cfdata->player;
   face->conf->theme = evas_stringshare_add(cfdata->themelist[cfdata->themenumber]);
   face->conf->input_charset = evas_stringshare_add(cfdata->charsetlist[cfdata->charsetselected]);
   
   e_config_save_queue();
   
   _eloquence_face_cb_config_updated(face);
   return 1;
}

